// 自动生成模板CasRetentionPersons
package retention_person

import (
	"slices"
	"time"

	"github.com/flipped-aurora/gin-vue-admin/server/consts"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"gorm.io/datatypes"
)

// casRetentionPersons表 结构体  CasRetentionPersons
type CasRetentionPersons struct {
	global.GVA_MODEL
	Name          *string        `json:"name" form:"name" gorm:"column:name;comment:姓名;size:191;" binding:"required"` //姓名
	Avatar        *string        `json:"avatar" form:"avatar" gorm:"column:avatar;comment:头像;size:191;"`              //头像
	IdNumber      *string        `json:"idNumber" form:"idNumber" gorm:"column:id_number;comment:身份证号;size:191;" binding:"required"`
	PassportNo    *string        `json:"passportNo" form:"passportNo" gorm:"column:passport_no;comment:护照号;size:191;"`           //护照号                                        //身份证号
	Address       *string        `json:"address" form:"address" gorm:"column:address;comment:户籍地址;size:191;" binding:"required"` //户籍地址
	Phone         *string        `json:"phone" form:"phone" gorm:"column:phone;comment:联系电话;size:191;"`                          //联系电话
	Age           *int           `json:"age" form:"age" gorm:"column:age;comment:年龄;size:10;"`                                   //年龄
	Gender        *string        `json:"gender" form:"gender" gorm:"column:gender;comment:性别;size:10;"`
	Nation        *string        `json:"nation" form:"nation" gorm:"column:nation;comment:民族;size:25;"` //民族
	Culture       *string        `json:"culture" form:"culture" gorm:"column:culture;comment:文化程度;size:25;"`
	MaritalStatus *string        `json:"maritalStatus" form:"maritalStatus" gorm:"column:marital_status;comment:婚姻状态;size:25;"`                  //婚姻状态
	Retention     *string        `json:"retention" form:"retention" gorm:"column:retention;comment:滞留地;size:25;"`                                //滞留地
	TownCode      *string        `json:"townCode" form:"townCode" gorm:"column:town_code;comment:上报乡镇;size:25;" binding:"required"`              //上报乡镇
	LiveTownCode  *string        `json:"liveTownCode" form:"liveTownCode" gorm:"column:live_town_code;comment:户籍乡镇;size:25;" binding:"required"` //户籍乡镇
	Tag           *string        `json:"tag" form:"tag" gorm:"column:tag;comment:标签;size:191;"`                                                  //标签
	Search        *string        `json:"search" form:"search" gorm:"column:search;comment:云搜截图;size:191;"`                                       //云搜截图
	Entry         datatypes.JSON `json:"entry" form:"entry" gorm:"column:entry;comment:出入境截图" swaggertype:"array,object"`
	Instruction   *string        `json:"instruction" form:"instruction" gorm:"column:instruction;comment:情况说明;"`                                                             //情况说明                                                                   //出入境截图
	ContactName   *string        `json:"contactName" form:"contactName" gorm:"column:contact_name;comment:联系人;size:25;"`                                                     //联系人
	ContactPhone  *string        `json:"contactPhone" form:"contactPhone" gorm:"column:contact_phone;comment:联系人电话;size:25;"`                                                //联系人电话
	State         *string        `json:"state" form:"state" gorm:"column:state;comment:状态 init初始化 search云搜 entry出入境 wait待上报 report已上报 drop不能上报;size:25;" binding:"required"` //状态 init初始化 search云搜 entry出入境 wait待上报 report已上报 drop不能上报
	Attachments   datatypes.JSON `json:"attachments" form:"attachments" gorm:"column:attachments;comment:附件" swaggertype:"array,object"`
	ActivateAt    *time.Time     `json:"activateAt" form:"activateAt" gorm:"column:activate_at;comment:最后活跃时间;"`            //活跃时间
	SubmitAt      *time.Time     `json:"submitAt" form:"submitAt" gorm:"column:submit_at;comment:提交时间;" binding:"required"` //提交时间
	ReportAt      *time.Time     `json:"reportAt" form:"reportAt" gorm:"column:report_at;comment:上报时间;"`                    //上报时间
	CreatedBy     uint           `gorm:"column:created_by;comment:创建者"`
	UpdatedBy     uint           `gorm:"column:updated_by;comment:更新者"`
	DeletedBy     uint           `gorm:"column:deleted_by;comment:删除者"`
}

func (c *CasRetentionPersons) FullColumns() {
	var state = consts.RetentionPersonStateInit
	if c.Instruction == nil || *c.Instruction == "" {
		state = consts.RetentionPersonStateInstruction
	}

	if len(c.Entry) == 0 || c.Entry.String() == "[]" {
		state = consts.RetentionPersonStateEntry
	}

	if c.Search == nil || *c.Search == "" {
		state = consts.RetentionPersonStateSearch
	}

	if len(c.Entry) > 0 && c.Search != nil && *c.Search != "" && c.Instruction != nil && *c.Instruction != "" {
		state = consts.RetentionPersonStateFileDone
	}

	if c.IsProfileComplete() {
		state = consts.RetentionPersonStateWait
	}

	// 如果状态不是已上报、不能上报、不予处理，则允许自动修改状态
	if !slices.Contains([]string{consts.RetentionPersonStateReport, consts.RetentionPersonStateDrop, consts.RetentionPersonStateReject}, *c.State) {
		c.State = &state
	}
}

func (c *CasRetentionPersons) IsProfileComplete() bool {
	if c.Avatar == nil || *c.Avatar == "" {
		return false
	}
	if c.IdNumber == nil || *c.IdNumber == "" {
		return false
	}
	if c.Name == nil || *c.Name == "" {
		return false
	}
	if c.Retention == nil || *c.Retention == "" {
		return false
	}
	if c.TownCode == nil || *c.TownCode == "" {
		return false
	}
	if c.LiveTownCode == nil || *c.LiveTownCode == "" {
		return false
	}
	if c.Nation == nil || *c.Nation == "" {
		return false
	}
	if c.Gender == nil || *c.Gender == "" {
		return false
	}
	if c.Address == nil || *c.Address == "" {
		return false
	}
	if c.Culture == nil || *c.Culture == "" {
		return false
	}
	if c.MaritalStatus == nil || *c.MaritalStatus == "" {
		return false
	}
	if c.ContactName == nil || *c.ContactName == "" {
		return false
	}
	if c.ContactPhone == nil || *c.ContactPhone == "" {
		return false
	}
	if c.Instruction == nil || *c.Instruction == "" {
		return false
	}
	if len(c.Entry) == 0 || c.Entry.String() == "[]" {
		return false
	}
	if c.Search == nil || *c.Search == "" {
		return false
	}

	return true
}

// TableName casRetentionPersons表 CasRetentionPersons自定义表名 cas_retention_persons
func (CasRetentionPersons) TableName() string {
	return "cas_retention_persons"
}
